<template>
	<el-dialog title="继电器开关" v-model="visible" :close-on-click-modal="false" draggable width="460px">
		<el-form :model="form" ref="dataFormRef" label-width="80px" :rules="dataRules">
			<el-form-item label="继电器" prop="relay">
				<el-select v-model="form.relay">
					<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
				</el-select>
			</el-form-item>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { useMessage } from '/@/hooks/message';
const options = [
	{
		label: 'K0',
		value: 0,
	},
	{
		label: 'K1',
		value: 1,
	},
	{
		label: 'K2',
		value: 2,
	},
	{
		label: 'K3',
		value: 3,
	},
	{
		label: 'K4',
		value: 4,
	},
	{
		label: 'K5',
		value: 5,
	},
	{
		label: 'K6',
		value: 6,
	},
	{
		label: 'K7',
		value: 7,
	},
];
const emit = defineEmits(['refresh']);
const visible = ref(false);
const loading = ref(false);
const dataFormRef = ref();
const form = reactive({
	relay: '',
});
// 打开弹窗
const openDialog = (id: string) => {
	visible.value = true;
	console.log(id);
};
const dataRules = ref({
	relay: [{ required: true, message: '请选择', trigger: 'change' }],
});

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;
	try {
		loading.value = true;
		// form.id ? await putObj(form) : await addObj(form);
		// useMessage().success(form.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};
// 暴露变量
defineExpose({
	openDialog,
});
</script>

<style lang="scss" scoped></style>
